import 'package:bxultimate/app/router/router_name.dart';
import 'package:bxultimate/app/theme/app_theme.dart';
import 'package:bxultimate/app/theme/gaps.dart';
import 'package:bxultimate/app/theme/text_theme/default_text_theme.dart';
import 'package:bxultimate/data/store/config_store.dart';
import 'package:bxultimate/resources/assets.dart';
import 'package:bxultimate/widgets/mk_card_container.dart';
import 'package:bxultimate/widgets/mk_list_cell_item.dart';
import 'package:bxultimate/widgets/mk_load_image.dart';
import 'package:bxultimate/widgets/mk_page.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:tdesign_flutter/tdesign_flutter.dart';

import 'controller.dart';

class OrganizationInformationPage extends StatefulWidget {
  const OrganizationInformationPage({Key? key}) : super(key: key);

  @override
  State<OrganizationInformationPage> createState() =>
      _OrganizationInformationPageState();
}

class _OrganizationInformationPageState
    extends State<OrganizationInformationPage> {
  final controller = Get.find<OrganizationInformationController>();
  final state = Get.find<OrganizationInformationController>().state;

  @override
  Widget build(BuildContext context) {
    return MkPage(
        child: Obx(()=>Scaffold(
          backgroundColor: Colors.transparent,
          appBar: TDNavBar(
            backgroundColor: Colors.transparent,
            title: '队伍信息',
            rightBarItems: [
              state.isAdmin ? TDNavBarItem(
                  iconWidget: Icon(Icons.more_horiz),
                  action: () {
                    showGeneralDialog(
                      context: context,
                      pageBuilder: (BuildContext buildContext,
                          Animation<double> animation,
                          Animation<double> secondaryAnimation) {
                        return TDAlertDialog.vertical(
                            title: '队伍操作',
                            showCloseButton: true,
                            buttons: [
                              TDDialogButtonOptions(
                                  title: '转让负责人',
                                  action: () {
                                    Navigator.pop(context);
                                    Get.toNamed(RouterName.organizationTransferAdmin,arguments: {
                                      'orgId': controller.orgId
                                    });
                                  },
                                  theme: TDButtonTheme.light),
                              TDDialogButtonOptions(
                                  title: '解散队伍',
                                  action: () {
                                    Navigator.pop(context);
                                    showGeneralDialog(
                                      context: context,
                                      pageBuilder: (BuildContext buildContext, Animation<double> animation,
                                          Animation<double> secondaryAnimation) {
                                        return TDAlertDialog(
                                          title: '确认解散队伍？',
                                          rightBtnAction: controller.deleteTeam,
                                        );
                                      },
                                    );
                                  },
                                  theme: TDButtonTheme.light),
                              TDDialogButtonOptions(
                                  title: '退出队伍',
                                  action: () {
                                    Navigator.pop(context);

                                    controller.outTeam();
                                  },
                                  theme: TDButtonTheme.light),
                            ]);
                      },
                    );
                  }): TDNavBarItem(),
            ],
          ),
          body: SingleChildScrollView(
            child: Obx(() => _buildBody()),
          ),
        )));
  }

  Widget _buildBody() {
    return Padding(
        padding: EdgeInsets.symmetric(horizontal: AppTheme.iDefaultPadding),
        child: Column(
          children: [
            Gaps.vGap12,
            Container(
              decoration: BoxDecoration(
                color: Colors.white,
                borderRadius: BorderRadius.circular(AppTheme.iDefaultRadius),
              ),
              child: ListTile(
                title: Text('${state.orgInfo.name}'),
                subtitle: Text('${state.orgInfo.address}'),
                leading: ClipRRect(
                  borderRadius: BorderRadius.circular(8),
                  child: MkLoadImage(
                    state.orgInfo.teamHeadImgUrl == null
                        ? Assets.imagesIcon
                        : ConfigStore.to.baseFileUrl +
                            state.orgInfo.teamHeadImgUrl!,
                    width: 50,
                    height: 50,
                    fit: BoxFit.cover,
                  ),
                ),
              ),
            ),
            Gaps.vGap12,
            Container(
              padding: EdgeInsets.only(bottom: AppTheme.iDefaultPadding),
              width: Get.width,
              decoration: BoxDecoration(
                color: Colors.white,
                borderRadius: BorderRadius.circular(AppTheme.iDefaultRadius),
              ),
              child: Column(
                crossAxisAlignment: CrossAxisAlignment.start,
                children: [
                  state.orgInfo.teamMemberInfos == null
                      ? SizedBox()
                      : Wrap(
                          alignment: WrapAlignment.start,
                          children: [
                            ...state.orgInfo.teamMemberInfos!.map((e) =>
                                _buildModuleItem(
                                    e.headPhoto == null
                                        ? Assets.imagesIcon
                                        : ConfigStore.to.baseFileUrl +
                                            e.headPhoto,
                                    '${e.name}', () {
                                  Get.toNamed(
                                      RouterName.organizationUserInformation,
                                      arguments: {
                                        "teamId": Get.arguments['orgId'],
                                        "idcard": e.idcard
                                      });
                                }))
                          ],
                        ),
                  Gaps.vGap8,
                  GestureDetector(
                    onTap: controller.toContactList,
                    child: Center(
                        child: Text(
                      '查看全部群成员(${state.orgInfo.teamMemberCount})',
                      style: TextStyle(fontSize: 12),
                    )),
                  )
                ],
              ),
            ),
            Gaps.vGap12,
            Obx(() => _buildNav()),
            // _buildActionMenu()
          ],
        ));
  }

  _buildNav() {
    return MkCardContainer(
      padding: EdgeInsets.symmetric(horizontal: AppTheme.iDefaultPadding / 2),
      margin: EdgeInsets.all(0),
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          Visibility(
            visible: state.isAdmin,
            child: MkListCellItem(
              value: '添加子队伍',
              press: () {
                Get.toNamed(RouterName.organizationCreate,
                    arguments: state.orgInfo.id);
              },
            ),
          ),
          MkListCellItem(
            value: '队伍名称',
            ext: Text('${state.orgInfo.name}'),
            press: () {},
          ),
          MkListCellItem(
            value: '队伍类型',
            ext: Text('${state.orgInfo.typeName}'),
            press: () {},
          ),
          MkListCellItem(
            value: '地理位置',
            ext:Padding(
              padding: EdgeInsets.only(left: 12),
              child: Text(
                '${state.orgInfo.address}',
                maxLines: 1,
                overflow: TextOverflow.ellipsis,
              ),
            ),
            press: () {},
          ),
          MkListCellItem(
            value: '队伍二维码',
            press: () {},
          ),
        ],
      ),
    );
  }

  _buildModuleItem(icon, name, onTap) {
    return GestureDetector(
      onTap: onTap,
      child: Container(
          width: (Get.width - AppTheme.iDefaultPadding * 2) / 5,
          padding: EdgeInsets.all(AppTheme.iDefaultPadding),
          child: Column(children: [
            ClipRRect(
              borderRadius: BorderRadius.circular(8),
              child: MkLoadImage(
                icon,
                width: 40,
                height: 40,
              ),
            ),
            Gaps.vGap4,
            Text(
              name,
              style: TextStyle(fontSize: 12),
            )
          ])),
    );
  }

  @override
  void dispose() {
    Get.delete<OrganizationInformationController>();
    super.dispose();
  }
}
